﻿/* Copyright (C) 2008 - 2011 by John Cronin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysos
{
    class Exceptions
    {
        [ABI.ISR]
        [ABI.AlwaysCompile]
        public static void DivideError_0_Handler()
        {
            Formatter.WriteLine("Divide error", Program.arch.BootInfoOutput);
            ABI.OtherOperations.Halt();
        }

        [ABI.ISR]
        [ABI.AlwaysCompile]
        public static void InvalidOpcode_6_Handler()
        {
            Formatter.WriteLine("Invalid opcode", Program.arch.BootInfoOutput);

            /* Unwind the stack */
            Formatter.WriteLine("Invalid opcode", Program.arch.DebugOutput);
            PageFault.unwinding = true;
            Unwind u = new Unwind();
            Formatter.WriteLine("Stack trace: ", Program.arch.DebugOutput);
            Formatter.WriteLine("Stack trace: ", Program.arch.BootInfoOutput);

            while (true)
                PageFault.DumpIP(u.UnwindOne());

            ABI.OtherOperations.Halt();
        }

        [ABI.ISR]
        [ABI.AlwaysCompile]
        public static void DoubleFault_8_Handler(ulong ec)
        {
            Formatter.WriteLine("Double fault", Program.arch.BootInfoOutput);
            ABI.OtherOperations.Halt();
        }

        [ABI.ISR]
        [ABI.AlwaysCompile]
        public static void GeneralProtection_13_Handler(ulong ec)
        {
            if (PageFault.unwinding)
                ABI.OtherOperations.Halt();

            Formatter.WriteLine("General protection error", Program.arch.BootInfoOutput);
            Formatter.WriteLine("General protection error", Program.arch.DebugOutput);

            /* Unwind the stack */
            PageFault.unwinding = true;
            Unwind u = new Unwind();
            Formatter.WriteLine("Stack trace: ", Program.arch.DebugOutput);

            PageFault.DumpIP(u.UnwindOneWithErrorCode());
            while (true)
                PageFault.DumpIP(u.UnwindOne());

            ABI.OtherOperations.Halt();
        }

        [ABI.ISR]
        [ABI.AlwaysCompile]
        public static void AlignmentCheck_17_Handler(ulong ec)
        {
            Formatter.WriteLine("Alignment check error", Program.arch.BootInfoOutput);
            ABI.OtherOperations.Halt();
        }

        [ABI.ISR]
        [ABI.AlwaysCompile]
        public static void SIMD_19_Handler()
        {
            if (PageFault.unwinding)
                ABI.OtherOperations.Halt();

            Formatter.WriteLine("SIMD error", Program.arch.BootInfoOutput);
            Formatter.Write("SIMD error, MXCSR: ", Program.arch.DebugOutput);
            Formatter.Write((ulong)ABI.OtherOperations.ReadMxcsr(), "X", Program.arch.DebugOutput);
            Formatter.WriteLine(Program.arch.DebugOutput);

            /* Unwind the stack */
            PageFault.unwinding = true;
            Unwind u = new Unwind();
            Formatter.WriteLine("Stack trace: ", Program.arch.DebugOutput);

            while (true)
                PageFault.DumpIP(u.UnwindOne());

            ABI.OtherOperations.Halt();
        }
    }
}
